<script setup>

import {getCurrentInstance, ref} from "vue";
import {deleteSysConfig, listSysConfig} from "@/api/system/config";
import SysConfigForm from "@/views/system/config/SysConfigForm.vue";

const {proxy} = getCurrentInstance();
const sysConfigList = ref([]);
const total = ref(0);
const sysConfigFormRef = ref();
const queryFormRef = ref();
const queryForm = ref({
  pageNo: 1,
  pageSize: 10,
  configName: null,
  configKey: null
})
const selectId = ref();

const getList = async () => {
  const {data} = await listSysConfig(queryForm.value)
  total.value = data.total;
  sysConfigList.value = data.list;
}

const openConfig = (configId, type) => {
  sysConfigFormRef.value.open(configId, type)
}

const deleteConfig = async (configId) => {
  proxy.$modal.confirm('确定删除该配置吗？').then(async () => {
    await deleteSysConfig(configId);
    proxy.$modal.okNotify('删除成功');
    await getList();
  })
}

const handleQuery = () => {
  queryForm.value.pageNo = 1;
  getList();
}

const handleReset = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

const handleSelectionChange = (rowKeys, rowKey, row) => {
  selectId.value = row.id
}

const handleUpdate = () => {
  if (!selectId.value) {
    proxy.$modal.warningNotify('请选择一条数据');
    return false;
  }
  openConfig(selectId.value, 'update');
}

const handleDelete = () => {
  if (!selectId.value) {
    proxy.$modal.warningNotify('请选择一条数据');
    return false;
  }
  deleteConfig(selectId.value)
}
getList();
</script>

<template>
  <div class="app-container">
    <a-form :model="queryForm" ref="queryFormRef" layout="inline" class="demo-form-inline">
      <a-form-item label="配置名称" field="configName">
        <a-input v-model="queryForm.configName" placeholder="请输入配置名称"/>
      </a-form-item>
      <a-form-item label="配置键名" field="configKey">
        <a-input v-model="queryForm.configKey" placeholder="请输入配置键名"/>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="handleQuery">
          <icon icon="ep-search" size="14px" color="unset" class="mr-1"/>
          查询
        </a-button>
        <a-button plain @click="handleReset" class="ml-12px">
          <icon icon="ep-refresh" size="14px" color="unset" class="mr-1"/>
          重置
        </a-button>
      </a-form-item>
    </a-form>

    <a-space class="flex mb-5">
      <a-button type="primary" plain class="flex items-center" @click="openConfig(null, 'create')"
                v-hasPermission="['system:config:add']">
        <icon icon="ep-plus" size="14px" color="unset" class="mr-1"/>
        新增
      </a-button>

      <a-button status="success" class="flex items-center" @click="handleUpdate"
                v-hasPermission="['system:config:edit']">
        <icon icon="ep-edit" size="14px" color="unset" class="mr-1"/>
        编辑
      </a-button>

      <a-button status="danger" class="flex items-center" @click="handleDelete"
                v-hasPermission="['system:config:delete']">
        <icon icon="ep-delete" size="14px" color="unset" class="mr-1"/>
        删除
      </a-button>
    </a-space>

    <a-table :data="sysConfigList" :bordered="true" size="large" :pagination="false"
             row-key="id"
             @select="handleSelectionChange"
             :row-selection="{
                type:'radio'
            }">
      <template #columns>
        <a-table-column data-index="id" title="参数ID"/>
        <a-table-column data-index="configName" title="参数名称"/>
        <a-table-column data-index="configKey" title="参数键名"/>
        <a-table-column data-index="configValue" title="参数值"/>
        <a-table-column data-index="remark" title="备注"/>
        <a-table-column fixed="right" title="操作" :width="200">
          <template #cell="{record}">
            <a-button class="ml-2" type="text" @click="openConfig(record.id,'update')"
                       v-hasPermission="['system:config:edit']">编辑
            </a-button>
            <a-button class="ml-2" status="danger" type="text" @click="deleteConfig(record.id)"
                       v-hasPermission="['system:config:delete']">
              删除
            </a-button>
          </template>
        </a-table-column>
      </template>
    </a-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryForm.pageNo"
        v-model:limit="queryForm.pageSize"
        @pagination="getList"/>

    <sys-config-form ref="sysConfigFormRef" @success="getList"/>
  </div>
</template>

<style scoped lang="scss">

</style>
